/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.kapilvirenahuja.eamsteps.datareader.excel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.Validate;

import jxl.Sheet;
import jxl.Workbook;

import com.kapilvirenahuja.eamsteps.datareader.IDataSet;
import com.kapilvirenahuja.eamsteps.datareader.IDataTable;

/**
 * @see IDataSet
 */
public class ExcelDataSet implements IDataSet
{
    private final Workbook workbook;

    public ExcelDataSet(Workbook workbook)
    {
        Validate.notNull(workbook);

        // If the validations pass, then set up the workbook locally.
        this.workbook = workbook;
    }

    public String[] getTablesNames()
    {
        String[] sheets = this.workbook.getSheetNames();
        if (sheets == null)
        {
            sheets = ArrayUtils.EMPTY_STRING_ARRAY;
        }

        return sheets;
    }

    public IDataTable getDataTable(String tableName)
    {
        Sheet sheet = this.workbook.getSheet(tableName);
        if (sheet != null)
        {
            return new ExcelDataTable(sheet);
        }

        return null;
    }

    public List<IDataTable> getDataTables()
    {
        List<IDataTable> tables = new ArrayList<IDataTable>();

        for (Sheet sheet : this.workbook.getSheets())
        {
            tables.add(new ExcelDataTable(sheet)); // NOPMD
        }

        return tables;
    }
}
